package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.SchoolbagGroupBagBooksDtoMapper;
import com.starlink.model.domain.SchoolbagGroupBagBooksDO;
import com.starlink.mapper.SchoolbagGroupBagBooksMapper;
import com.starlink.model.request.SchoolbagGroupBagBooksRequest;
import com.starlink.service.SchoolbagGroupBagBooksService;
import com.starlink.model.util.SchoolbagGroupBagBooksConvert;
import com.starlink.model.vo.SchoolbagGroupBagBooksVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class SchoolbagGroupBagBooksServiceImpl extends ServiceImpl<SchoolbagGroupBagBooksMapper, SchoolbagGroupBagBooksDO> implements SchoolbagGroupBagBooksService {
    @Autowired
    private SchoolbagGroupBagBooksMapper schoolbagGroupBagBooksMapper;

    @Override
    public SchoolbagGroupBagBooksVO getByKey(long id) {
        SchoolbagGroupBagBooksVO schoolbagGroupBagBooksVO = SchoolbagGroupBagBooksDtoMapper.INSTANCE.do2VO(getById(id));
        return schoolbagGroupBagBooksVO;
    }

    @Override
    public PageInfo<SchoolbagGroupBagBooksVO> pageQuery(Pageable pageable,SchoolbagGroupBagBooksRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<SchoolbagGroupBagBooksDO> list = schoolbagGroupBagBooksMapper.listQuery(request);
        List<SchoolbagGroupBagBooksVO> schoolbagGroupBagBooksVOList = SchoolbagGroupBagBooksConvert.buildSchoolbagGroupBagBooksVoList(list);
        return new PageInfo(schoolbagGroupBagBooksVOList);
    }

    @Override
    public List<SchoolbagGroupBagBooksVO> listQuery(SchoolbagGroupBagBooksRequest request) {
        List<SchoolbagGroupBagBooksDO> list = schoolbagGroupBagBooksMapper.selectDOList(SchoolbagGroupBagBooksDtoMapper.INSTANCE.request2DO(request));
        List<SchoolbagGroupBagBooksVO> schoolbagGroupBagBooksVOS = SchoolbagGroupBagBooksConvert.buildSchoolbagGroupBagBooksVoList(list);
        return schoolbagGroupBagBooksVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(SchoolbagGroupBagBooksRequest request) {
        save(SchoolbagGroupBagBooksDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(SchoolbagGroupBagBooksRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(SchoolbagGroupBagBooksDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        schoolbagGroupBagBooksMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }
}
